<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: article_content_manager.php 16 2013-09-21 11:52:15Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 16 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:52:15 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
require_once(DIR_FS_INC.'inc.format_filesize.php');
require_once(DIR_FS_INC.'inc.get_url_friendly_text.php');

function getFileSize($file, $setup = null) {
	if(is_file($file)) {
	    $FZ = ($file && is_file($file)) ? filesize($file) : NULL;
	    $FS = array("B","kB","MB","GB","TB","PB","EB","ZB","YB");
	    
	    if(!$setup && $setup !== 0)
	        return number_format($FZ/pow(1024, $I=floor(log($FZ, 1024))), ($i >= 1) ? 2 : 0) . ' ' . $FS[$I];
	    elseif ($setup == 'INT') 
	    	return number_format($FZ);
	    else 
	    	return number_format($FZ/pow(1024, $setup), ($setup >= 1) ? 2 : 0 ). ' ' . $FS[$setup];
	 }
}

if(MODULE_COMMERCE_SEO_INDEX_STATUS == 'True') {
  	require_once (DIR_FS_INC.'inc.seo_url.php');
	!$seo_url ? $seo_url = new SeoUrl() : false;
}

$languages = get_languages();

if($_GET['special']=='delete_product')
	$db->db_query("DELETE FROM ".TABLE_PRODUCTS_CONTENT." WHERE content_id = '".(int)$_GET['coID']."'");

if($_GET['id']=='update_product' || $_GET['id']=='insert_product') {
	$group_ids='';
	if(isset($_POST['groups'])) {
		foreach($_POST['groups'] AS $b)
			$group_ids .= 'c_'.$b."_group,";
	}
	$customers_statuses_array = get_customers_statuses();
	if (strstr($group_ids,'c_all_group')) {
		$group_ids='c_all_group,';
		for ($i=0;$n=sizeof($customers_statuses_array),$i<$n;$i++) {
			$group_ids .='c_'.$customers_statuses_array[$i]['id'].'_group,';
		}
	}

	$content_title = $_POST['cont_title'];
	$content_link = $_POST['cont_link'];
	$content_language = $_POST['language'];
	$product = $_POST['product'];
	$upload_file = $_POST['file_upload'];
	$filename = $_POST['file_name'];
	$coID = $_POST['coID'];
	$file_comment = stripslashes($_POST['file_comment']);
	$select_file = $_POST['select_file'];
	$group_ids = $group_ids;

	$error = false;

	for($i = 0, $n = sizeof($languages); $i < $n; $i++) {
		if($languages[$i]['code'] == $content_language)
			$content_language=$languages[$i]['id'];
	}

	if(strlen($content_title) < 1) {
		$error = true;
		$message_stack->add(ERROR_TITLE,'error');
	}

	if($error == false) {
		if ($select_file == 'default') {
			if ($content_file = &try_upload('file_upload', DIR_FS_CATALOG.'media/products/')) {
				$old_filename = $content_file->filename;
				$suffix_tmp = explode('.', $content_file->filename);
				$suffix = array_reverse($suffix_tmp);
				$content_file_name = get_url_friendly_text($suffix[1]).'.'.$suffix[0];
				$rename_string = DIR_FS_CATALOG.'media/products/'.$content_file_name;
				rename(DIR_FS_CATALOG.'media/products/'.$old_filename, $rename_string);
				copy($rename_string,DIR_FS_CATALOG.'media/products/backup/'.$content_file_name);
			}
			if ($content_file_name=='')
				$content_file_name = $filename;

		} else
			$content_file_name = $select_file;

		$group_ids='';
		if(isset($_POST['groups']))
			foreach($_POST['groups'] as $b){
				$group_ids .= 'c_'.$b."_group,";
		}
		$customers_statuses_array = get_customers_statuses();
		if(strstr($group_ids,'c_all_group')) {
			$group_ids='c_all_group,';
			for ($i=0;$n=sizeof($customers_statuses_array),$i<$n;$i++)
				$group_ids .='c_'.$customers_statuses_array[$i]['id'].'_group,';
		}

		$sql_data_array = array('products_id' => $product,
								'group_ids' => $group_ids,
								'content_name' => $content_title,
								'content_file' => $content_file_name,
								'content_link' => $content_link,
								'file_comment' => $file_comment,
								'languages_id' => $content_language);

		if ($_GET['id']=='update_product') {
			$db->db_perform(TABLE_PRODUCTS_CONTENT, $sql_data_array, 'update', "content_id = '".$coID."'");
			$content_id = $coID;
		} else {
			$db->db_perform(TABLE_PRODUCTS_CONTENT, $sql_data_array);
			$content_id = $db->db_insert_id();
		}
		redirect(href_link(FILENAME_ARTICEL_CONTENT_MANAGER, 'pID='.$content_id));
	}
}
require('includes/metatag.php'); ?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php');?>
<div id="wrapper">
	<table class="outerTable" cellpadding="0" cellspacing="0">
		<tr>
			<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
				<?php require(DIR_WS_INCLUDES.'column_left.php');?>
			</td>
			<td class="boxCenter" valign="top">
				<table border="0" width="100%" cellspacing="0" cellpadding="0">
					<tr>
						<td>
							<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
								<tr>
									<td class="pageHeading"><?php echo HEADING_TITLE;?></td>
								</tr>
							</table>
							<table width="100%">
								<tr><td><p><?php echo TEXT_CONTENT_DESCRIPTION; ?></p></td></tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<table width="100%" border="0">
								<tr>
									<td>
										<?php
										if ($_GET['action']=='edit_products_content' || $_GET['action']=='new_products_content') { 
											if ($_GET['action']=='edit_products_content') {
												$content = $db->db_query("SELECT
																				content_id,
																				products_id,
																				group_ids,
																				content_name,
																				content_file,
																				content_link,
																				languages_id,
																				file_comment,
																				content_read
																			FROM
																				".TABLE_PRODUCTS_CONTENT."
																			WHERE
																				content_id='".(int)$_GET['coID']."'");
											}

											$products_data = $db->db_query("SELECT
																				products_id,
																				products_name
																				FROM
																				".TABLE_PRODUCTS_DESCRIPTION."
																				WHERE
																				language_id='".(int)$_SESSION['languages_id']."'");
											$products_array=array();

											while (!$products_data->EOF) {

												$products_array[] = array('id' => $products_data->fields['products_id'], 'text' => $products_data->fields['products_name']);

												$products_data->MoveNext();
											}

											$languages_array = array();

											for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {

												if ($languages[$i]['id']==$content->fields['languages_id']) {
													$languages_selected=$languages[$i]['code'];
													$languages_id=$languages[$i]['id'];
												}
													$languages_array[] = array('id' => $languages[$i]['code'], 'text' => $languages[$i]['name']);

											}

											$content_files_data = $db->db_query("SELECT DISTINCT
																					content_name,
																					content_file
																				FROM
																					".TABLE_PRODUCTS_CONTENT."
																				WHERE
																					content_file!=''");
											$content_files = array();

											while (!$content_files_data->EOF) {
												$content_files[] = array('id' => $content_files_data->fields['content_file'], 'text' => $content_files_data->fields['content_name']);

												$content_files_data->MoveNext();
											}

											$default_array[] = array('id' => 'default', 'text' => TEXT_SELECT);
											$default_value = 'default';
											$content_files = array_merge($default_array,$content_files);

											if ($_GET['action'] != 'new_products_content')
												echo draw_form('edit_content',FILENAME_ARTICEL_CONTENT_MANAGER,'action=edit_products_content&id=update_product&coID='.$_GET['coID'],'post','enctype="multipart/form-data"').draw_hidden_field('coID',$_GET['coID']);
											else
												echo draw_form('edit_content',FILENAME_ARTICEL_CONTENT_MANAGER,'action=edit_products_content&id=insert_product','post','enctype="multipart/form-data"');
											?>
												<table class="main" width="100%" border="0">
													<tr>
													<td width="10%"><?php echo TEXT_PRODUCT; ?></td>
													<td width="90%"><?php echo draw_pull_down_menu('product',$products_array,$content->fields['products_id']); ?></td>
													</tr>
													<tr>
													<td width="10%"><?php echo TEXT_LANGUAGE; ?></td>
													<td width="90%"><?php echo draw_pull_down_menu('language',$languages_array,$languages_selected); ?></td>
													</tr>

													<?php
													if (GROUP_CHECK=='true') {
														$customers_statuses_array = get_customers_statuses();
														$customers_statuses_array = array_merge(array(array('id'=>'all','text'=>TXT_ALL)),$customers_statuses_array);
													?>
													<tr>
														<td style="border: 1px solid #b20000; border-width: 1px 0 1px 1px; background:#FFE9A6" valign="top">
															<?php echo ENTRY_CUSTOMERS_STATUS; ?>
														</td>
														<td style="border: 1px solid #b20000; border-width: 1px 1px 1px 0; background:#FFE9A6">
														<?php
	
														for ($i=0; $n=sizeof($customers_statuses_array), $i<$n ;$i++) {
															if (strstr($content->fields['group_ids'],'c_'.$customers_statuses_array[$i]['id'].'_group'))
																$checked = 'checked ';
															else
																$checked = '';
															
															echo '<input type="checkbox" name="groups[]" value="'.$customers_statuses_array[$i]['id'].'"'.$checked.' /> '.$customers_statuses_array[$i]['text'].'<br />';
														}
														?>
														</td>
													</tr>
													<?php
													}
													?>

													<tr>
														<td width="10%"><?php echo TEXT_TITLE_FILE; ?></td>
														<td width="90%"><?php echo draw_input_field('cont_title',$content->fields['content_name'],'size="60"'); ?></td>
													</tr>
													<tr>
														<td width="10%"><?php echo TEXT_LINK; ?></td>
														<td width="90%"><?php  echo draw_input_field('cont_link',$content->fields['content_link'],'size="60"'); ?></td>
													</tr>
													<tr>
														<td width="10%" valign="top"><?php echo TEXT_FILE_DESC; ?></td>
														<td width="90%">
															<?php echo wysiwyg('file_comment', $content->fields['file_comment']); ?>
														</td>
													</tr>
													<tr>
														<td width="10%"><?php echo TEXT_CHOOSE_FILE; ?></td>
														<td width="90%"><?php echo draw_pull_down_menu('select_file',$content_files,$default_value); ?><?php echo ' '.TEXT_CHOOSE_FILE_DESC; ?></td>
													</tr>
													<tr>
													<td width="10%" valign="top"><?php echo TEXT_UPLOAD_FILE; ?></td>
													<td width="90%"><?php echo draw_file_field('file_upload').' '.TEXT_UPLOAD_FILE_LOCAL; ?></td>
													</tr>
													<?php
														if ($content->fields['content_file']!='') {
													?>
													<tr>
													<td width="10%"><?php echo TEXT_FILENAME; ?></td>
													<td width="90%" valign="top">
														<?php echo draw_hidden_field('file_name',$content->fields['content_file']).image(DIR_WS_CATALOG.'admin/images/icons/icon_'.str_replace('.','',strstr($content->fields['content_file'],'.')).'.gif').' '.$content->fields['content_file']; ?></td>
													</tr>
													<?php
													}
													?>
													<tr>
														<td colspan="2" align="right" class="main">
															<button type="submit" class="button"><?php echo BUTTON_SAVE; ?></button> 
															<a class="button" onclick="this.blur();" href="<?php echo href_link(FILENAME_ARTICEL_CONTENT_MANAGER); ?>">
																<?php echo BUTTON_BACK; ?>
															</a>
														</td>
													</tr>
												</table>
											</form>
									<?php } ?>
									</td>
								</tr>
							</table>
							<?php
								if (!$_GET['action']) {
									if(!$_GET['action'])
										echo '<br /><a class="button" onclick="this.blur();" href="'.href_link(FILENAME_ARTICEL_CONTENT_MANAGER,'action=new_products_content').'">'.BUTTON_NEW_CONTENT.'</a>';
									$products_id_data = $db->db_query("	SELECT DISTINCT
																			pc.products_id,
																			pd.products_name
																		FROM
																			".TABLE_PRODUCTS_CONTENT." pc, ".TABLE_PRODUCTS_DESCRIPTION." pd
																		WHERE
																			pd.products_id = pc.products_id
																		AND
																			pd.language_id='".(int)$_SESSION['languages_id']."'");

									$products_ids=array();
									while(!$products_id_data->EOF) {
										$products_ids[]=array('id'=>$products_id_data->fields['products_id'], 'name'=>$products_id_data->fields['products_name']);
										$products_id_data->MoveNext();
									} ?>

									<div class="pageHeading"><br /><?php echo HEADING_PRODUCTS_CONTENT; ?><br /></div>
									<?php
										spaceUsed(DIR_FS_CATALOG.'media/products/');
										echo '<div class="main">'.USED_SPACE.format_filesize($total).'</div></br>';
									?>
							<table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
								<tr class="dataTableHeadingRow">
									<td class="dataTableHeadingContent" nowrap width="5%" ><?php echo TABLE_HEADING_PRODUCTS_ID; ?></td>
									<td class="dataTableHeadingContent" align="left" colspan="9"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
								</tr>
								<?php
								for ($i=0,$n=sizeof($products_ids); $i<$n; $i++) {
									if ($ii % 2 == 0)
									$f = 'dataTableRow';
									else
									$f = '';
									echo '<tr class="'.$f.'" onmouseover="this.className=\'dataTableRowOver\'" onmouseout="this.className=\''.$f.'\'">'."\n";
									?>
									<td class="dataTableContent_products" align="left"><?php echo $products_ids[$i]['id']; ?></td>
									<td class="dataTableContent_products" align="left" colspan="9"><b><?php echo image('images/icon_arrow_right.gif'); ?> <a href="<?php echo href_link(FILENAME_ARTICEL_CONTENT_MANAGER,'pID='.$products_ids[$i]['id']);?>"><?php echo $products_ids[$i]['name']; ?></a></b><br />
									
									<?php
									if ($_GET['pID']) {
										$content_data = $db->db_query("SELECT
																			content_id,
																			content_name,
																			content_file,
																			content_link,
																			languages_id,
																			file_comment,
																			content_read
																		FROM
																			".TABLE_PRODUCTS_CONTENT."
																		WHERE
																			products_id='".$_GET['pID']."'
																		ORDER BY
																			content_name");
										$content_array='';
										while (!$content_data->EOF) {
											$content_array[]=array('id'=> $content_data->fields['content_id'],
																	'name'=> $content_data->fields['content_name'],
																	'file'=> $content_data->fields['content_file'],
																	'link'=> $content_data->fields['content_link'],
																	'comment'=> $content_data->fields['file_comment'],
																	'languages_id'=> $content_data->fields['languages_id'],
																	'read'=> $content_data->fields['content_read']);
											$content_data->MoveNext();
										}
										if ($_GET['pID']==$products_ids[$i]['id']) {
											?>
											<table width="100%">
											<tr class="dataTableHeadingRow">
												<td>&nbsp;</td>
												<td class="dataTableHeadingContent" nowrap><?php echo TABLE_HEADING_PRODUCTS_CONTENT_ID; ?></td>
												<td class="dataTableHeadingContent" nowrap >&nbsp;</td>
												<td class="dataTableHeadingContent" nowrap ><?php echo TABLE_HEADING_LANGUAGE; ?></td>
												<td class="dataTableHeadingContent" nowrap ><?php echo TABLE_HEADING_CONTENT_NAME; ?></td>
												<td class="dataTableHeadingContent" nowrap><?php echo TABLE_HEADING_CONTENT_FILE; ?></td>
												<td class="dataTableHeadingContent" nowrap><?php echo TABLE_HEADING_CONTENT_FILESIZE; ?></td>
												<td class="dataTableHeadingContent" nowrap align="middle" width="20%" ><?php echo TABLE_HEADING_CONTENT_LINK; ?></td>
												<td class="dataTableHeadingContent" nowrap><?php echo TABLE_HEADING_CONTENT_HITS; ?></td>
												<td class="dataTableHeadingContent last" nowrap ><?php echo TABLE_HEADING_CONTENT_ACTION; ?></td>
											</tr>

											<?php
											for ($ii=0,$nn=sizeof($content_array); $ii<$nn; $ii++) {
												if ($ii % 2 == 0)
													$f = 'dataTableRow';
												else
													$f = '';
												echo ''."\n";
												?>
												<tr class="<?php echo $f; ?>" onmouseover="this.className='dataTableRowOver'" onmouseout="this.className='<?php echo $f; ?>'">
													<td>&nbsp;</td>
													<td align="left"><?php echo  $content_array[$ii]['id']; ?> </td>
													<td align="left" class="last">
														<?php
															if ($content_array[$ii]['file']!='') {
																$suff_tmp = explode('.', $content_array[$ii]['file']);
																$suffix = array_reverse($suff_tmp);
																echo image(DIR_WS_CATALOG.'admin/images/icons/icon_'.$suffix[0].'.gif');
																
															} else
																echo image(DIR_WS_CATALOG.'admin/images/icons/icon_link.gif');

															for ($xx=0,$zz=sizeof($languages); $xx<$zz; $xx++){
																if ($languages[$xx]['id'] == $content_array[$ii]['languages_id']) {
																	$lang_dir = $languages[$xx]['directory'];
																	$lang_img = $languages[$xx]['image'];
																	break;
																}
															}
														?>
													</td>
													<td align="center"><?php echo image(DIR_WS_CATALOG.'lang/'.$lang_dir.'/'.$lang_img); ?></td>
													<td align="left"><?php echo $content_array[$ii]['name']; ?></td>
													<td align="left"><?php echo $content_array[$ii]['file']; ?></td>
													<td align="center"><?php echo getFileSize(DIR_FS_CATALOG.'media/products/'.$content_array[$ii]['file']); ?></td>
													<td align="left" align="middle">
														<?php
															if ($content_array[$ii]['link']!='')
																echo '<a href="'.$content_array[$ii]['link'].'" target="new">'.$content_array[$ii]['link'].'</a>';
														?>&nbsp;
													</td>
													<td align="center"><?php echo $content_array[$ii]['read']; ?></td>
													<td align="right" class="last">
														<a href="<?php echo href_link(FILENAME_ARTICEL_CONTENT_MANAGER,'special=delete_product&coID='.$content_array[$ii]['id']).'&pID='.$products_ids[$i]['id']; ?>" onclick="return confirm('<?php echo CONFIRM_DELETE; ?>')">
															<?php
																echo image(DIR_WS_ICONS.'delete.gif', TEXT_DELETE,'','','style="cursor:pointer" onclick="return confirm(\''.DELETE_ENTRY.'\')"');
															?>
														</a> 
														<a href="<?php echo href_link(FILENAME_ARTICEL_CONTENT_MANAGER,'action=edit_products_content&coID='.$content_array[$ii]['id']); ?>">
															<?php echo image(DIR_WS_ICONS.'document--pencil.png', TEXT_EDIT,'','','style="cursor:pointer"'); ?>
														</a>
														<?php
														if (preg_match('/.gif/i',$content_array[$ii]['file'])
														or
														preg_match('/.jpg/i',$content_array[$ii]['file'])
														or
														preg_match('/.png/i',$content_array[$ii]['file'])
														or
														preg_match('/.html/i',$content_array[$ii]['file'])
														or
														preg_match('/.php/i',$content_array[$ii]['file'])
														or
														preg_match('/.htm/i',$content_array[$ii]['file'])
														or
														preg_match('/.avi/i',$content_array[$ii]['file'])
														or
														preg_match('/.txt/i',$content_array[$ii]['file'])
														or
														preg_match('/.doc/i',$content_array[$ii]['file'])
														or
														preg_match('/.bmp/i',$content_array[$ii]['file'])
														) {
															?>
															<a style="cursor:pointer" onclick="window.open('<?php echo href_link(FILENAME_CONTENT_PREVIEW,'pID=media&coID='.$content_array[$ii]['id']); ?>', 'popup', 'toolbar=0, width=640, height=600')">
																<?php echo image(DIR_WS_ICONS.'preview.gif','Preview','','',' style="cursor:pointer"').'&nbsp;&nbsp;'.TEXT_PREVIEW; ?>
															</a>
															<?php
														}
														?>
													</td>
												</tr>
												<?php
											}
										} ?>
										</table>
									<?php }
								}
							?>
							</table>
							<br />
							<a class="button" onclick="this.blur();" href="<?php echo href_link(FILENAME_ARTICEL_CONTENT_MANAGER,'action=new_products_content'); ?>"><?php echo BUTTON_NEW_CONTENT; ?></a>
							<?php
							}
							?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>